package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func GetHandler(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "GET",
	})
}

func PostHandler(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "POST",
	})
}

func DeleteHandler(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "DELETE",
	})
}

func main() {

	r := gin.Default()

	//同一组 posts 接口分开写
	/*
		r.GET("/posts", GetHandler)

		r.POST("/posts", PostHandler)

		//根据id进行删除
		r.DELETE("/posts/:id", DeleteHandler)
	*/

	//同一组 posts 接口放一组
	p := r.Group("/posts")
	{
		p.GET("/", GetHandler)

		p.POST("/", PostHandler)

		//根据id进行删除
		p.DELETE("/:id", DeleteHandler)
	}

	//http://localhost:8080/posts/v1
	//http://localhost:8080/posts/v2
	ps := r.Group("/posts")
	{
		//版本 v1 的接口
		v1 := ps.Group("/v1")
		{
			v1.GET("/", GetHandler)

			v1.POST("/", PostHandler)

			//根据id进行删除
			v1.DELETE("/:id", DeleteHandler)
		}
		//版本 v2 的接口
		v2 := ps.Group("/v2")
		{
			v2.GET("/", GetHandler)

			v2.POST("/", PostHandler)

			//根据id进行删除
			v2.DELETE("/:id", DeleteHandler)
		}
	}

	r.Run()
}
